package com.toy.controller.admin;


import com.toy.entity.OrderDetail;
import com.toy.entity.Orders;
import com.toy.entity.Product;
import com.toy.entity.User;
import com.toy.exception.ToyMallException;
import com.toy.mapper.OrderDetailMapper;
import com.toy.mapper.OrdersMapper;
import com.toy.mapper.ProductMapper;
import com.toy.mapper.UserMapper;
import com.toy.result.ResponseEnum;
import com.toy.service.OrderDetailService;
import com.toy.service.OrdersService;
import com.toy.vo.OrderListVO;
import com.toy.vo.OrdersVO;
import com.toy.vo.PageResult;
import com.toy.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-08-31
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrderDetailController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;
    /**
     * 返回当前用户的订单列表
     * @return
     */
    @GetMapping("/getOrderList")
    public SysResult ordersList(PageResult pageResult){
        PageResult orderByPage = ordersService.findOrderByPage(pageResult);
        List<Orders> orderList = ordersService.selectOrderList(orderByPage);
        ArrayList<OrderListVO> orderListVOS = new ArrayList<>();
        for (Orders orders : orderList) {
            Integer id = orders.getId();
            OrderDetail orderDetail = orderDetailMapper.selectById(id);
            if (Objects.isNull(orderDetail)||orderDetail.getStatus() == 0){
                continue;
            }
            Integer productId = orderDetail.getProductId();
            Product product = productMapper.selectById(productId);
            Integer userId = orders.getUserId();
            User user = userMapper.selectById(userId);
            OrderListVO orderListVO = new OrderListVO();
            orderListVO.setId(orders.getId());
            orderListVO.setStatus(orderDetail.getStatus());
            orderListVO.setCost(orderDetail.getCost());
            orderListVO.setLoginName(orders.getLoginName());
            orderListVO.setProductName(product.getName());
            orderListVO.setProductFileName(product.getFileName());
            orderListVO.setUserAddress(orders.getUserAddress());
            orderListVO.setSerialnumber(orders.getSerialnumber());
            orderListVO.setProductQuantity(orderDetail.getQuantity());
            orderListVOS.add(orderListVO);
        }
        orderByPage.setRows(null);
        orderByPage.setRows(orderListVOS);
        return SysResult.success(orderByPage);
    }

    @PutMapping("/updateProduct")
    public SysResult updateProduct(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return SysResult.success();
    }
    @GetMapping("/{id}")
    public SysResult findOrderById(@PathVariable Integer id){
        Orders orders = ordersService.selectOrderById(id);
        return SysResult.success(orders);
    }
    @DeleteMapping("/{id}")
    public SysResult deleteItemById(@PathVariable Integer id){
        ordersService.removeById(id);
        orderDetailMapper.deleteById(id);
        return SysResult.success();
    }
    @PutMapping("/updateItemStatus")
    public SysResult updateItemStatus(@RequestBody OrderDetail orderDetail){
        orderDetail.setStatus(2);
        orderDetailMapper.updateById(orderDetail);
        return SysResult.success();
    }
}

